package com.kycq.viewpager.simple;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kycq.viewpager.R;
import com.kycq.viewpager.databinding.ItemSimplePagerBinding;

public class SimplePagerAdapter extends PagerAdapter {
	private LayoutInflater mInflater;

	public SimplePagerAdapter(Context context) {
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return 5;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		return DataBindingUtil.inflate(mInflater, R.layout.item_simple_pager, container, true);
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		ItemSimplePagerBinding dataBinding = (ItemSimplePagerBinding) object;
		container.removeView(dataBinding.getRoot());
	}

	@Override
	public void setPrimaryItem(ViewGroup container, int position, Object object) {
		ItemSimplePagerBinding dataBinding = (ItemSimplePagerBinding) object;
		dataBinding.tvContent.setText(getPageTitle(position));
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		ItemSimplePagerBinding dataBinding = (ItemSimplePagerBinding) object;
		return dataBinding.getRoot() == view;
	}

	@Override
	public CharSequence getPageTitle(int position) {
		return String.valueOf(position + 1);
	}

	@Override
	public float getPageWidth(int position) {
		if (position == 0) {
			return 0.3f;
		}
		return 1f;
	}
}
